﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MapGenerator
{
    public partial class Save : Form
    {
        List<List<int>> mapGrid;
        public Save(List<List<int>> mapGrid_)
        {
            InitializeComponent();
            mapGrid = mapGrid_;
            LoadMapText();
        }

        private void LoadMapText()
        {
            string mapText = "{";
            foreach(var rows in mapGrid)
            {
                mapText += "\r\n{";
                foreach(int i in rows)
                {
                    mapText += i.ToString() + ",";
                }
                mapText = mapText.Remove(mapText.Length - 1);
                mapText += "},";
            }
            mapText = mapText.Remove(mapText.Length - 1);
            mapText += "\r\n};";
            tbMapData.Text = mapText;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "Map files (*.maps)|*.maps|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.InitialDirectory = Environment.CurrentDirectory;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.Stream fileStream = saveFileDialog1.OpenFile();
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
                sw.WriteLine(tbMapData.Text);
                sw.Flush();
                sw.Close();
            }
        }

        private void btToClipBoard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(tbMapData.Text, TextDataFormat.Html);
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
